'''
Created on 2013-1-11

@author: hanchen
'''
# encoding: utf-8

import urllib
import urllib2

FOUND = 0
NOT_FOUND = 1
FAIL = -1

def str_error(errno):
    result = ""
    if errno == FOUND:
        result = "found";
    elif errno == NOT_FOUND:
        result = "not found"
    else:
        result = "fail"
    
    return result
        

def is_fb_user(username):
    url = "https://www.facebook.com/ajax/login/help/identify.php?ctx=recover"
    #conn = httplib.HTTPConnection("127.0.0.1", 8087)
    body = urllib.urlencode({
                    "lsd": "AVp_3idm",
                    "email": username,
                    "your_name": "",
                    "friend_name": "",
                    "did_submit": "Search",
                    "__user: ": "0",
                    "__a": 1,
                    "__req": 2,
                    "fb_dtsg": "AQDHcRIW",
                    "phstamp": "16581687299827387119"
                });
    headers = { 
               "Content-Type": "application/x-www-form-urlencoded"
               };
    # proxy = httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP, "127.0.0.1", 8087)
    # http = httplib2.Http() # (proxy_info=proxy)
    # response = http.request(url, "POST", body, headers);
    # print response.status, response.reason
    # data = response[1]
    request = urllib2.Request(url, body, headers);
    response = urllib2.urlopen(request)
    data = response.read()
    #print data
    if data.find("No Accounts Found") >= 0:
        # print "Not Found", username
        return NOT_FOUND
    else:
        # print "Found", username
        return FOUND
    
    return FAIL

def main():
    print str_error(is_fb_user("hcconquer@gmail.com"))
    print str_error(is_fb_user("hcconquer@qq.com"))

if __name__ == '__main__':
    main()
    